package com.yanqu.road.dao.impl.channelcenter.tonbingchannel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.tonbingchannel.join.ChannelTonBingUnionJoin;
import com.yanqu.road.entity.activity.tonbingchannel.join.ChannelTonBingUnionJoinMember;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelTonBingUnionJoinDaoImpl extends TempDataObjectDao<ChannelTonBingUnionJoin> {

    public static String getChannelTonBingUnionJoinSql = "select * from `t_n_tonbing_union_join` where `activityId`=?";

    public Map<String, ChannelTonBingUnionJoin> getChannelTonBingUnionJoinMap(int activityId) {
        Map<String, ChannelTonBingUnionJoin> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(getChannelTonBingUnionJoinSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelTonBingUnionJoin parse = parse(rs);
                map.put(parse.getUnionUid(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateChannelTonBingUnionJoinSql = "update `t_n_tonbing_union_join` set unionBaseInfo=?,memberMap=?,campId=?,totalAbility=? where activityId=? and unionUid=?";

    @Override
    protected boolean updateData(ChannelTonBingUnionJoin crossTonBingUnionJoin) {
        crossTonBingUnionJoin.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUnionJoin.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUnionJoin.getMemberMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUnionJoin.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUnionJoin.getTotalAbility()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUnionJoin.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossTonBingUnionJoin.getUnionUid()));
        if (getDb().executeNoneQuery(updateChannelTonBingUnionJoinSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertChannelTonBingUnionJoinSql = "insert into `t_n_tonbing_union_join`(activityId,unionUid,unionBaseInfo,memberMap,campId,totalAbility) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(ChannelTonBingUnionJoin crossTonBingUnionJoin) {
        crossTonBingUnionJoin.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUnionJoin.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossTonBingUnionJoin.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUnionJoin.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUnionJoin.getMemberMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUnionJoin.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUnionJoin.getTotalAbility()));
        if (getDb().executeNoneQuery(insertChannelTonBingUnionJoinSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ChannelTonBingUnionJoin parseData(ResultSet rs) throws SQLException {
        ChannelTonBingUnionJoin crossTonBingUnionJoin = new ChannelTonBingUnionJoin();
        crossTonBingUnionJoin.setActivityId(rs.getInt("activityId"));
        crossTonBingUnionJoin.setUnionUid(rs.getString("unionUid"));
        crossTonBingUnionJoin.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), UnionBaseInfo.class));
        crossTonBingUnionJoin.setMemberMap(JSON.parseObject(rs.getString("memberMap"), new TypeReference<ConcurrentHashMap<Long, ChannelTonBingUnionJoinMember>>() {
        }));
        crossTonBingUnionJoin.setCampId(rs.getInt("campId"));
        crossTonBingUnionJoin.setTotalAbility(rs.getLong("totalAbility"));
        return crossTonBingUnionJoin;
    }

}
